﻿
var currentId = null;
var Ischannel = null;
// PageLoad function
  // This function is called when:
  // 1. after calling $.historyInit();
  // 2. after calling $.historyLoad();
  // 3. after pushing "Go Back" button of a browser
  function pageload(hash) {
    // hash doesn't contain the first # character.
   var step = "1"; //第几页
   var PageSize="24" ; //每页显示几个商品
   if(document.getElementById("PageSize") != null) 
    {
        PageSize = document.getElementById("PageSize").value;
    }
   var DisplayMethod="1" ;
   if(document.getElementById("DisplayMethod") != null) 
    {
        DisplayMethod = document.getElementById("DisplayMethod").value;
    }
   var Sort=null;
   var Order=null;
   var SortMethod=document.getElementById("SortMethod"); //排序
   if(SortMethod!=null)
   {
       if(SortMethod.selectedIndex>-1)
       {
           Sort=SortMethod.options[SortMethod.selectedIndex].value;
           if(SortMethod.selectedIndex%2==0)
           {
               Order="2"; 
           }
           else
           {
               Order="1"; 
           }
       }
   }
    if(hash) {
      var paras = hash.split("-");
      var groupID = paras[0]
      PageSize = decodeURIComponent(paras[1]);
      step = decodeURIComponent(paras[2]);
      DisplayMethod = paras[3];     
      Sort = paras[4];
      Order=paras[5];
      //deubgger;
      $j.post("../Handler/ProductsHtmlHandler.ashx", { CategoryID: groupID,PageSize:PageSize,Page:step,ShowMethod:DisplayMethod,Sort:Sort,Order:Order,IsChannel:Ischannel},
            function(data){ 
                document.getElementById("ListArea").innerHTML= data;
                window.document.title=document.getElementById("TypePath").value;
            }
        );
      
    } else {
        $j.post("../Handler/ProductsHtmlHandler.ashx", { CategoryID: currentId,PageSize:PageSize,Page:step,ShowMethod:DisplayMethod,Sort:Sort,Order:Order,IsChannel:Ischannel},
            function(data){ 
                document.getElementById("ListArea").innerHTML= data;
                window.document.title=document.getElementById("TypePath").value;
            }
        );
    }
  }
function showProductList(groupID,channelflag)
{
     document.getElementById("ListArea").innerHTML = "<br /><br />数据加载中......<br /><br /><img src='../images/ajax-loader.gif'>";	    
     currentId = groupID;
     Ischannel = channelflag;
 }
function initPage()
 {
    // Initialize history plugin.
    // The callback is called at once by present location.hash. 
    $j.historyInit(pageload);    
    // set onlick event for buttons
    $j("a[@rel='history']").click(function(){
      // 
      var hash = this.href;
      hash = hash.replace(/^.*#/, '');
      // moves to a new page. 
      // pageload is called at once. 
      $j.historyLoad(hash);
      return false;
    });
}
//------改变展现方式--------------
function ChangeShowMethod(showmethod,category)
{   
    var ShowMethod;
    if(document.getElementById("DisplayMethod")!=null) 
    {
        ShowMethod=document.getElementById("DisplayMethod").value;
    }
    if(ShowMethod!=showmethod)
    {
        if (document.getElementById("tmpImg")!= null)
                document.getElementById("tmpImg").style.display = "block";
        else
                document.getElementById("ListArea").innerHTML = "<br /><br />数据加载中......<br /><br /><img src='../images/ajax-loader.gif'>";
        var PageSize="12";
        if(document.getElementById("PageSize") != null) 
        {
            PageSize = document.getElementById("PageSize").value;
        }
        var Page="1";
        if(document.getElementById("Page")!=null)
        {
            Page=document.getElementById("Page").value;
        }
       var Sort=null;
       var Order=null;
       var SortMethod=document.getElementById("SortMethod"); //排序
       if(SortMethod!=null)
       {
         if(SortMethod.selectedIndex>-1)
         {
           Sort=SortMethod.options[SortMethod.selectedIndex].value;
           if(SortMethod.selectedIndex%2==0)
           {
               Order="2"; 
           }
           else
           {
               Order="1"; 
           }
         }
       }
      loadCompoundURL(category,PageSize,Page,showmethod,Sort,Order); 
    }
}
//----------------------改变展现方式结束-----------------------
//---------------------改变显示数量------------------------------
function ChangePageSize(pagesize,category)
{
    var PageSize;
    if(document.getElementById("PageSize")!=null)
    {
        PageSize=document.getElementById("PageSize").value;
    }
    if(PageSize!=pagesize)
    {
        if (document.getElementById("tmpImg")!= null)
                document.getElementById("tmpImg").style.display = "block";
        else
                document.getElementById("ListArea").innerHTML = "<br /><br />数据加载中......<br /><br /><img src='../images/ajax-loader.gif'>";
        var ShowMethod;
        if(document.getElementById("DisplayMethod")!=null) 
        {
        ShowMethod=document.getElementById("DisplayMethod").value;
        }
        var Page="1";
        var Sort=null;
        var Order=null;
        var SortMethod=document.getElementById("SortMethod"); //排序
        if(SortMethod!=null)
        {
            if(SortMethod.selectedIndex>-1)
            {
                Sort=SortMethod.options[SortMethod.selectedIndex].value;
                if(SortMethod.selectedIndex%2==0)
                {
                    Order="2"; 
                }
                else
                {
                    Order="1"; 
                }
            }
        }
        loadCompoundURL(category,pagesize,Page,ShowMethod,Sort,Order);
    }
}
//--------------------------改变页显示数量结束--------------------------------
//--------------------------排序---------------------------
function ChangeSort(sort,category)
{
    var Sort;
    var Order;
    var SortMethod=document.getElementById("SortMethod");
    if(SortMethod!=null)
    {
        if(SortMethod.selectedIndex>-1)
            {
                Sort=document.getElementById("Sort").value;
                if(SortMethod.selectedIndex%2==0)
                {
                    Order="2"; 
                }
                else
                {
                    Order="1"; 
                }
            }
    }
    if(Sort!=sort)
    {
        if (document.getElementById("tmpImg")!= null)
                document.getElementById("tmpImg").style.display = "block";
        else
                document.getElementById("ListArea").innerHTML = "<br /><br />数据加载中......<br /><br /><img src='../images/ajax-loader.gif'>";
        var PageSize;
        if(document.getElementById("PageSize")!=null) 
        {
            PageSize=document.getElementById("PageSize").value;
        }
        var ShowMethod;
        if(document.getElementById("DisplayMethod")!=null) 
        {
            ShowMethod=document.getElementById("DisplayMethod").value;
        }
        var Page="1";
        loadCompoundURL(category,PageSize,Page,ShowMethod,sort,Order);
    }
}
//--------------------------排序结束--------------------
//-------------------------翻页-------------------------
function ChangePage(index,pagecount,category)
{
    var pattern=/^[0-9]*[1-9][0-9]*$/;
    var flag=pattern.test(index);
    if(!flag)
    {
        alert("请输入正整数");
        document.getElementById("turnPage").focus();
        return;
    }
    if(index<1) index=1;
    if(index>pagecount) index=pagecount;
    if (document.getElementById("tmpImg")!= null)
                document.getElementById("tmpImg").style.display = "block";
        else
                document.getElementById("ListArea").innerHTML = "<br /><br />数据加载中......<br /><br /><img src='../images/ajax-loader.gif'>";
    var Sort;
    var Order;
    var SortMethod=document.getElementById("SortMethod");
    if(SortMethod!=null)
    {
        if(SortMethod.selectedIndex>-1)
            {
                Sort=document.getElementById("Sort").value;
                if(SortMethod.selectedIndex%2==0)
                {
                    Order="2"; 
                }
                else
                {
                    Order="1"; 
                }
            }
    }    
    var PageSize;
    if(document.getElementById("PageSize")!=null) 
    {
        PageSize=document.getElementById("PageSize").value;
    }
    var ShowMethod;
    if(document.getElementById("DisplayMethod")!=null) 
    {
        ShowMethod=document.getElementById("DisplayMethod").value;
    }
    var Page=index;
    loadCompoundURL(category,PageSize,Page,ShowMethod,Sort,Order);
}
//-------------------------翻页结束---------------------
function loadCompoundURL(category,ps,pg,smd,st,or)
   {
        var strUrl=category; 
        strUrl += "-" + ps + "-" + pg + "-" + smd + "-" + st+"-"+or;
        $j.historyLoad(strUrl);
   }